package com.itboye.ihomebank.login;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.RegisterBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.controller.LoginController;
import com.itboye.ihomebank.controller.LoginedState;
import com.itboye.ihomebank.me.ActivitySetting;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 注册第3步（设置密码）
 * 
 */
public class RegisterActivityThree extends BaseOtherActivity implements
		OnClickListener, Observer {
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题

	private EditText res_three_num;// 密码
	private TextView res_three_ok;// 完成并登陆
	private Intent intent;
	ToggleButton res_three_tb;

	private String phone;// 接上页面传来的手机号
	private String yzm;// 接上页面传来的验证码

	UserPresenter userPresenter;
	KeyPresenter keyPresenter;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();

			break;
		case R.id.res_three_ok:
			UserRegister();
			break;
		case R.id.res_three_tb:// 密码可见
			if (res_three_tb.isChecked()) {
				res_three_num
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				res_three_num.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);

			}
			break;

		default:
			break;
		}

	}

	/**
	 * 注册
	 */
	private void UserRegister() {

		if (TextUtils.isEmpty(res_three_num.getText().toString())) {
			ByAlert.alert("请输入密码");
			return;
		}
		userPresenter = new UserPresenter(this);
		userPresenter.register("3", "0", yzm, phone, res_three_num.getText()
				.toString());

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == UserPresenter.register_success) {
					RegisterBean bean = (RegisterBean) entity.getData();
					Log.i("result", bean.toString());
					if (bean != null) {
						// SPUtils.put(RegisterActivityThreeThree.this, null,
						// SPContants.USER_ID, response);
						// SPUtils.put(RegisterActivityThreeThree.this, null,
						// SPContants.USERNAME,phone);

						// SPUtils.put(RegisterActivityThreeThree.this, null,
						// SPContants.MOBILE, phone);
						// SPUtils.put(App.ctx, null, SPContants.PASSWORD,
						// passwordedit.getText().toString());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.IS_LOGINED, true);
						login();
						

						RegisterActivityThree.this.finish();

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());

						ByAlert.alert("注册成功");

					}
				}
				if (entity.getEventType() == UserPresenter.register_fail) {
					ByAlert.alert("注册失败");
				}
				if (entity.getEventType() == UserPresenter.user_success) {
					PersonDataBean bean = (PersonDataBean) entity.getData();
					if (bean != null) {
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.USERNAME, bean.getUsername());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.MOBILE, bean.getMobile());
						if (bean.getMobile() != null) {
							if (bean.getMobile().equals("")) {
								SPUtils.put(RegisterActivityThree.this, null,
										SPContants.IS_BIND_PHONE, false);
							} else {
								SPUtils.put(RegisterActivityThree.this, null,
										SPContants.IS_BIND_PHONE, true);
							}
						} else {
							SPUtils.put(RegisterActivityThree.this, null,
									SPContants.IS_BIND_PHONE, false);
						}
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.IS_LOGINED, true);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.EMAIL,
								bean.getEmail());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.NICK,
								bean.getNickname());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.PASSWORD, bean.getPassword());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.USER_ID, bean.getUid());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.AVATAR_URL, bean.getHead());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.SEX,
								bean.getSex());// 性别
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.IdentityValidate,// '实名身份认证【0未认证,1已认证,2待审核】'
								bean.getIdentityValidate());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.BrokerValidate,
								bean.getBrokerValidate());// 经纪人认证【0未认证,1已认证,2待审核】
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.ScienerUsername,// 绑定科技侠账号，科技侠账号,为空未绑定
								bean.getScienerUsername());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.IdNumBer,// 身份证号码
								bean.getIdnumber());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.WalletPass,//钱包支付密码 
								bean.getWalletPassword());
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.RealName, bean.getRealname());// 身份证号码
						SPUtils.put(this, null, SPContants.BaichuanUID,
								bean.getBaichuanUid());// 阿里百川即时通讯 的uid

						if (bean.getWalletPassword() != null) {// 支付密码
							if (bean.getWalletPassword().equals("")) {
								SPUtils.put(RegisterActivityThree.this, null,
										SPContants.IS_ZHIFU_PASS, false);
							} else {
								SPUtils.put(RegisterActivityThree.this, null,
										SPContants.IS_ZHIFU_PASS, true);
							}
						} else {
							SPUtils.put(RegisterActivityThree.this, null,
									SPContants.IS_ZHIFU_PASS, false);
						}
						userPresenter.bindKeJiXia(bean.getUid(),true);//直接注册科技侠
						Intent intent1 = new Intent(ActivitySetting.EXITCHANGE);
						sendBroadcast(intent1);
					}
				} else if (entity.getEventType() == UserPresenter.bindKeJiXia_success) {
					//ByAlert.alert(entity.getData());
				} else if (entity.getEventType() == UserPresenter.bindKeJiXia_fail) {
					//ByAlert.alert(entity.getData());
				}
			}
		}

	}

	private void bingKeJiXia() {
		// TODO Auto-generated method stub
		String uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		String name=SPUtils.get(this, null, SPContants.USERNAME, "") + "";
		String pass=SPUtils.get(this, null, SPContants.PASSWORD, "") + "";
		Log.v("user",uid+"::"+name+"::"+pass+"");
		userPresenter.bindKeJiXia(uid,true);
	}

	/**
	 * 登录
	 */
	public void login() {
		keyPresenter=new KeyPresenter(this);
		userPresenter = new UserPresenter(this);
		userPresenter.login(phone, res_three_num.getText().toString());
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_register_three;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("设置密码");

		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");
		
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	/*
	 * private void UserRegister() {//注册 if
	 * (res_three_num.getText().toString().trim() == null) {
	 * T.showLong(RegisterActivityThree.this, "请输入验证码"); return; }
	 * MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
	 * .apiVer("100").typeKey("BY_User_register") .param("from", "0")
	 * .param("reg_type", "3") .param("username", phone) .param("password",
	 * res_three_num.getText().toString()) .requestListener(new
	 * XRequestListener<String>() {
	 * 
	 * @Override public void onResponse(String response) { // TODO
	 * Auto-generated method stub DebugLog.v("result", response.toString());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID,
	 * response); SPUtils.put(RegisterActivityThree.this, null,
	 * SPContants.USERNAME,phone);
	 * 
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE, phone);
	 * // SPUtils.put(App.ctx, null, SPContants.PASSWORD, //
	 * passwordedit.getText().toString());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED,
	 * true);
	 * 
	 * login();
	 * 
	 * RegisterActivityThree.this.finish();
	 * 
	 * // 发送广播通知RegisterActivityThree结束 Intent intent1 = new
	 * Intent(LoginActivity.LOGINEXIT); sendBroadcast(intent1);
	 * LoginController.setLoginState(new LoginedState());
	 * 
	 * } }).errorListener(new XErrorListener() {
	 * 
	 * @Override public void onErrorResponse(Exception exception, int code,
	 * String msg) { if (exception instanceof CodeErrorException) {
	 * T.showLong(RegisterActivityThree.this, msg); } else {
	 * T.showLong(RegisterActivityThree.this, exception.toString()); }
	 * 
	 * } }).build(); HttpRequest.getDefaultRequestQueue().add(request); }
	 */

	/*
	 * public void login() {//登录 MyJsonRequest<PersonDataBean> request = new
	 * MyJsonRequest.Builder<PersonDataBean>() .apiVer("100")
	 * .typeKey("BY_User_login") .param("username",phone)
	 * .param("password",res_three_num.getText().toString())
	 * .requestListener(new XRequestListener<PersonDataBean>() {
	 * 
	 * @Override public void onResponse(PersonDataBean response) {
	 * DebugLog.v("result", response.toString());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.WX_Openid,
	 * response.getWxopenid()); SPUtils.put(RegisterActivityThree.this, null,
	 * SPContants.USERNAME, response.getUsername());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE,
	 * response.getMobile()); SPUtils.put(RegisterActivityThree.this, null,
	 * SPContants.IS_LOGINED, true); SPUtils.put(RegisterActivityThree.this,
	 * null, SPContants.EMAIL, response.getEmail());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.NICK,
	 * response.getNickname()); SPUtils.put(RegisterActivityThree.this, null,
	 * SPContants.PASSWORD, response.getPassword());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID,
	 * response.getUid()); SPUtils.put(RegisterActivityThree.this, null,
	 * SPContants.AVATAR_URL, response.getHead());
	 * SPUtils.put(RegisterActivityThree.this, null, SPContants.AQ_NUM,
	 * response.getAqCount()); Intent intent1 = new Intent(
	 * ActivitySetting.EXITCHANGE); sendBroadcast(intent1); }
	 * }).errorListener(new XErrorListener() {
	 * 
	 * @Override public void onErrorResponse(Exception exception, int code,
	 * String msg) { if (exception instanceof CodeErrorException) {
	 * T.showShort(RegisterActivityThree.this, msg); } else {
	 * T.showShort(RegisterActivityThree.this, exception.toString()); } }
	 * }).build(); HttpRequest.getDefaultRequestQueue().add(request); }
	 */
}
